package com.ysqcould.web.controller.curriculum;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ysqcould.common.annotation.Log;
import com.ysqcould.common.core.controller.BaseController;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.core.page.TableDataInfo;
import com.ysqcould.common.enums.BusinessType;
import com.ysqcould.common.oss.OssVideoUtil;
import com.ysqcould.common.utils.poi.ExcelUtil;
import com.ysqcould.curriculum.domain.Course;
import com.ysqcould.curriculum.domain.CourseTag;
import com.ysqcould.curriculum.domain.CourseTeacher;
import com.ysqcould.curriculum.domain.CourseType;
import com.ysqcould.curriculum.service.ICourseService;
import com.ysqcould.curriculum.service.ICourseTagService;
import com.ysqcould.curriculum.service.ICourseTeacherService;
import com.ysqcould.curriculum.service.ICourseTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 课程视频Controller
 * 
 * @author jcd
 * @date 2022-09-27
 */
@RestController
@RequestMapping("/curriculum/course")
public class CourseController extends BaseController
{
    @Autowired
    private ICourseService courseService;

    @Autowired
    private ICourseTagService courseTagService;

    @Autowired
    private ICourseTeacherService teacherService;

    @Autowired
    private ICourseTypeService typeService;

    /**
     * 查询课程视频列表
     */
    @PreAuthorize("@ss.hasPermi('curriculum:course:list')")
    @GetMapping("/list")
    public AjaxResult list(Course course)
    {
        Long asId=null;
        Long userId = getUserId();
        if (userId==1){
            userId=asId;
        }
        AjaxResult result=AjaxResult.success();
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        List<CourseTag> tag = courseTagService.selectList(userId);
        List<CourseTeacher> teacher = teacherService.selectList(userId);
        List<CourseType> type = typeService.selectList(userId);
        result.put("list",getDataTable(list));
        result.put("tag",tag);
        result.put("teacher",teacher);
        result.put("type",type);
        return result;
    }

    /**
     * 导出课程视频列表
     */
    @PreAuthorize("@ss.hasPermi('curriculum:course:export')")
    @Log(title = "课程视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course)
    {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程视频数据");
    }

    /**
     * 获取课程视频详细信息
     */
    @PreAuthorize("@ss.hasPermi('curriculum:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(courseService.selectCourseById(id));
    }

    /**
     * 新增课程视频
     */
    @PreAuthorize("@ss.hasPermi('curriculum:course:add')")
    @Log(title = "课程视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course)
    {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程视频
     */
    @PreAuthorize("@ss.hasPermi('curriculum:course:edit')")
    @Log(title = "课程视频", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程视频
     */
    @PreAuthorize("@ss.hasPermi('curriculum:course:remove')")
    @Log(title = "课程视频", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(courseService.deleteCourseByIds(ids));
    }

    @Log(title = "课程视频上传",businessType = BusinessType.DELETE)
    @PostMapping("/uploadVideo")
    public AjaxResult getUrl(MultipartFile file) throws Exception {
        JSONObject object = OssVideoUtil.uploadStream(file.getOriginalFilename(), file.getInputStream());
        return AjaxResult.success(object);
    }

    @Log(title = "课程标签",businessType = BusinessType.GRANT)
    @PostMapping("/listType")
    public AjaxResult listType(){
       return AjaxResult.success( courseService.getType(getUserId()));
    }

    @Log(title = "课程分类",businessType = BusinessType.GRANT)
    @PostMapping("/listTag")
    public AjaxResult listTag(){
        return AjaxResult.success(courseService.getTag(getUserId()))
        ;
    }

    @Log(title = "课程讲师",businessType = BusinessType.GRANT)
    @PostMapping("/listTeacher")
    public AjaxResult listTeacher(){
        return  AjaxResult.success(courseService.getTeacher(getUserId()));
    }

    @Log(title = "课程视频",businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Course course){
        return toAjax(courseService.changeStatus(course));
    }

    @Log(title = "课程视频",businessType = BusinessType.UPDATE)
    @PostMapping("changeIsShow")
    public AjaxResult changeIsShow(@RequestBody Course course){
      return   toAjax(courseService.changeIsShow(course));
    }
}
